<?php
namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use DB;
use Hash;
use App\Libraries\ValidateCode;
class LoginController extends Controller
{
    //登录
    public function login()
    {
        //查询所有登录广告图片
        $arr = DB::table('carousel') -> where('class','=',1)->where('disabled','=',1) -> get();
    	return view('/home/login/login')->with(['arr'=>$arr]);
    }

    //处理登录（post提交的用Request $request）
    public function doLogin(Request $request)
    {
        $data = $request -> except('_token','code');
        $this->validate($request, [
            //验证规则，required(不为空)
             'name' => 'required',
            'password' => 'required',
            'code' => 'required',
        ],[
             //错误提醒
             'name.required' => '请输入账号',
             'password.required' => '请输入密码',
             'code.required' => '请输入验证码',             
        ]);
        
        // 检测验证码
        $code = $request -> input('code');
        // dd(session('code'));
        $request -> flash();      
        if($code != session('code'))
        {
            return back() -> with(['error' => '验证码错误']);
        }

        //验证账号
        $name = DB::table('member') -> lists('name');//把name字段以数组的形式查出来
        if(!in_array($data['name'],$name))
        {
            return back() -> with(['error' => '账户名与密码不匹配，请重新输入']);
        }


       //判断该会员是否被禁用
        $disabled = DB::table('member') -> where('name',$data['name']) -> first() -> disabled;
        if($disabled == 2)
        {
            return '<script>alert("您已被禁用");window.location.href="/login";</script>';
        }


        //查询密码
        $p = DB::table('member') -> where('name','=',$data['name']) -> first() -> password;
        //验证密码
        if (Hash::check($data['password'], $p)) {

            //登入成功
            $arr = DB::table('member') ->where('name',$data['name'])->first(); 
            $time = time();
            $logged = 2;          
            DB::table('member') -> where('name',$data['name']) -> update(['ltime' => $time,'logged' => $logged]);

            session(['name'=>$data['name']]);
            session(['member_id'=>$arr->member_id]);
            session(['nickname'=>$arr->nickname]);
            session(['pic'=>$arr->pic]);

            session(['permission'=>$arr->permission]);
            session(['ltime' => $time]);
            session(['logged' => $logged]);
            session(['phone' => $arr ->phone]);
            session(['email' => $arr ->email]);
            return redirect('/');

        }else
        {
            //登录失败
            return back() -> with(['error' => '账户名与密码不匹配，请重新输入']);
        }
    }

    //生成验证码
    public function v_code($id)
    {   
        $vc=new ValidateCode();
        $vc->doimg();
        session(['code' => $vc->getCode()]);        
    }

    //退出网站时，清除session 内所有数据
    public function unsetSession(Request $request)
    {
        $logged = 1;
        DB::table('member') -> where('member_id',session('member_id')) -> update(['logged' => $logged]);
        $request->session()->forget('name');
        $request->session()->forget('member_id');
        $request->session()->forget('nickname');
        $request->session()->forget('pic');
        $request->session()->forget('permission');
        $request->session()->forget('ltime');
        $request->session()->forget('phone');
        $request->session()->forget('logged');
        return redirect('/login');
    }
}
